from flask_restful import Resource

from apps.API.auth.models import Users
from apps.API.auth.util import verify_pass,hash_pass
from apps.API.connector.db_common import *
from flask import request,jsonify
from flask_jwt_extended import jwt_required
from apps import db
import re

class interfaceResetUserPasswd(Resource):
    def _is_password_strong(self,password):
        if len(password) < 8:
            return False
        if not re.search("[a-z]", password):
            return False
        if not re.search("[A-Z]", password):
            return False
        if not re.search("[0-9]", password):
            return False
        if not re.search("[!@#$%^&*(),.?\":{}|<>]", password):
            return False
        return True
    @jwt_required()
    def post(self):
        try:
            data = request.get_json()
            if not data:
                response=jsonify({"error": "No data provided"})
                response.status=400
                return response
            username = data.get('username')
            oldPassWord = data.get('oldPassWord')
            newPassWord = data.get('newPassWord')
            
            # 验证必要字段
            if not all([username, oldPassWord, newPassWord]):
                response=jsonify({"error": "Missing required fields"})
                response.status=400
                return response
            
            user = Users.query.filter(Users.username==username).first()
            # 检查用户是否存在
            if not user:
                response=jsonify({"error": "User not found"})
                response.status=404
                return response

            # # 验证旧密码
            if not verify_pass(str(oldPassWord),user.password):
                response=jsonify({"error": "旧密码错误！！"})
                response.status=403
                return response
            
            # # 检查新旧密码是否相同
            if oldPassWord == newPassWord:
                response=jsonify({"error": "新旧密码不能一样！！"})
                response.status=400
                return response
            
            # 验证密码强度
            # if not self._is_password_strong(newPassWord):
            #     return jsonify({
            #         "error": "Password not strong enough",
            #         "requirements": {
            #             "min_length": 8,
            #             "needs_lowercase": True,
            #             "needs_uppercase": True,
            #             "needs_digit": True,
            #             "needs_special_char": True
            #         }
            #     }), 400
            
            # 更新密码
            newPassWord=hash_pass(newPassWord)
            Users.query.filter(Users.username==username).update({"password":newPassWord})
            db.session.commit()
            response=jsonify({})
            response.status=202
            return response
        except Exception as e:
              response=jsonify({})
              response.status=400
